package com.cdj.common.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.cdj.common.R;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;

/**
 * ---------------------------------------------------------<br />
 * desc：基础的列表<br />
 * author：陈德基 <br />
 * date：2021/7/5 0:40<br />
 * email：18701434169@163.com<br />
 * qq: 781571323
 * wx: melody_2009
 * ---------------------------------------------------------<br />
 */
public class CommonListView<T> extends RecyclerView implements IAdapterHolder<T> {

    private BaseQuickAdapter<T, BaseViewHolder> adapter;

    private IAdapterChildData<T> adapterChildData;

    public void setAdapterChildData(IAdapterChildData<T> adapterChildData) {
        this.adapterChildData = adapterChildData;
    }

    public CommonListView(@NonNull Context context) {
        this(context, null);
    }

    public CommonListView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CommonListView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray arr = context.obtainStyledAttributes(attrs, R.styleable.CommonListView);
        int layoutID = arr.getResourceId(R.styleable.CommonListView_item_layout_id, 0);
        arr.recycle();

        adapter = new BaseQuickAdapter<T, BaseViewHolder>(layoutID) {
            @Override
            protected void convert(BaseViewHolder helper, T item) {
                if (adapterChildData != null) {
                    adapterChildData.fillDataToView(helper, item);
                }
            }
        };

//        View emptyView = LayoutInflater.from(context).inflate(R.layout.empty_layout, null, false);
//        adapter.setEmptyView(emptyView);
        setAdapter(adapter);
        setLayoutManager(new LinearLayoutManager(context));
        initHolder();
    }

    protected void initHolder() {

    }

//    protected abstract void fillItemData(BaseViewHolder helper, T item);

    @Override
    public BaseQuickAdapter<T, BaseViewHolder> adapter() {
        return adapter;
    }
}
